<?php

namespace App\Jobs;

use App\Models\Message;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class OrderExpire implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $order;

    /**
     * CloseOrder constructor.
     * @param $order
     */
    public function __construct($order)
    {
        $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (Order::$PROCEDURE_STEP_MAP[$this->order->status] >= Order::$PROCEDURE_STEP_MAP[Order::STATUS_CONFIRM]) {
            return;
        }

        $this->order->refund_status = Order::REFUND_STATUS_APPLIED;

        $this->order->save();

        Message::create([
            'user_id' => $this->order->user_id,
            'title' => trans('api.order_message'),
            'content' => trans('api.confirm_expired_message'),
            'type' => Message::ORDER,
        ]);
    }

    public function failed(\Exception $e)
    {
        // 发送失败通知, etc...
    }
}
